package com.cn.unarmed.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;

import com.cn.unarmed.activity.R;
import com.cn.unarmed.commonutils.AppUtils;
import com.cn.unarmed.views.BaseTitleLayout;
import com.readystatesoftware.systembartint.SystemBarTintManager;

/**
 * 所有activity都继承这个
 */
@SuppressWarnings("NewApi")
public abstract class BaseActivity extends Activity{

    /**
     * 枚举titleBar的值
     */
    public enum TitleBar {
        LEFT,
        RIGHT,
        TITLE,
        SHARE,
        RCV
    }

    public BaseTitleLayout layout;

    public Animation shake ;
    SystemBarTintManager tintManager;

//   手势监听
    GestureDetector mGestureDetector;
//    是否需要关闭手势监听
    boolean mNeedBackGesture = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        shake = AnimationUtils.loadAnimation(this, R.anim.shake);
//        sdk编译版本大于19
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            setTranslucentStatus(true);
        }
        tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setTintColor(getResources().getColor(R.color.bg));
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        try {
            Window win = getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            if (on) {
                winParams.flags |= bits;
            } else {
                winParams.flags &= ~bits;
            }
            win.setAttributes(winParams);

        } catch (Exception e) {
            Log.i("Exception","Exception");
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mNeedBackGesture){
            return mGestureDetector.onTouchEvent(ev)
                    ||super.dispatchTouchEvent(ev);
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
//      设置动画，进入activity与退出activity,，第一个参数是进入事的动画，第二个是退出时的动画
        overridePendingTransition(R.anim.slide_right_in,R.anim.slide_left_out);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);

        if (layout == null){
            layout = new BaseTitleLayout(this,layoutResID);
        }
//      设置布局时考虑系统窗口
        layout.setFitsSystemWindows(true);
        super.setContentView(layout);
        this.setClickListener(new View []{layout.left,layout.right,layout.title,
                layout.tv_meshare,layout.tv_mercv});
    }

    @Override
    public void setTitle(CharSequence title) {

        if(title.length()>12){
            layout.title.setTextSize(AppUtils.px2dip(this,8));
        }

        if (!TextUtils.isEmpty(title))
            layout.title.setText(title);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
//            AppUtils.logger("OnClickListener",view.getId()+"");
            if (view.equals(layout.left)) {
//                AppUtils.logger("OnClickListener","left");
                HandleTitleBarEvent(TitleBar.LEFT, view);
            } else if (view.equals(layout.right)) {
                HandleTitleBarEvent(TitleBar.RIGHT, view);
            } else if (view.equals(layout.title)) {
                HandleTitleBarEvent(TitleBar.TITLE, view);
            }else if (view.equals(layout.tv_meshare)) {
                HandleTitleBarEvent(TitleBar.SHARE, view);
            }else if (view.equals(layout.tv_mercv)) {
                HandleTitleBarEvent(TitleBar.RCV, view);
            }
        }
    };

    /**
     * @param component titleBar的值
     * @param v 视图组件
     */
    protected abstract void HandleTitleBarEvent(TitleBar component, View v);

    /**
     * @param views 设置监听事件
     */
    private void setClickListener(View[] views) {
        for (View v : views) {
            v.setOnClickListener(listener);
            hideSoftInput(v);
        }
    }

    /**
     * 隐藏软键
     *
     * @param v 软键盘视图
     */
    public void hideSoftInput(View v) {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }

    public void setShareBtn(){
        layout.tv_meshare.setTextColor(getResources().getColor(R.color.text_color));
        layout.tv_mercv.setTextColor(getResources().getColor(R.color.text_name));
    }

    public void setRcvBtn(){
        layout.tv_mercv.setTextColor(getResources().getColor(R.color.text_color));
        layout.tv_meshare.setTextColor(getResources().getColor(R.color.text_name));
    }

    public void setNavBtn(int leftBtnDraw,int rightBtnDraw){
        if (layout!=null){
            setSingleNavBtn(layout.left,leftBtnDraw);
            setSingleNavBtn(layout.right,rightBtnDraw);
            layout.lay_text.setVisibility(View.GONE);
        }
    }

    public void setNavBtn(int leftBtnDraw, String leftVal, int rightBtnDraw, String rightVal) {
        if (layout != null) {
            setSingleNavBtn(layout.left, leftBtnDraw, leftVal);
            setSingleNavBtn(layout.right, rightBtnDraw, rightVal);
            layout.lay_text.setVisibility(View.VISIBLE);
        }
    }

    public void setNavBtn(int leftBtnDraw, String leftVal, int meshareBtnDraw, String meshareVal,int mercvBtnDraw,String mercvVal) {
        if (layout != null) {
            setSingleNavBtn(layout.left, leftBtnDraw, leftVal);
            setSingleNavText(layout.tv_meshare, meshareBtnDraw, meshareVal);
            setSingleNavText(layout.tv_mercv, mercvBtnDraw, mercvVal);
            layout.lay_text.setVisibility(View.VISIBLE);
        }
    }

    private void setSingleNavText(TextView btn, int res, String value) {
        if (res > 0) {
            btn.setBackgroundResource(res);
        } else if (!TextUtils.isEmpty(value)) {
            btn.setText(value);
        } else {
            btn.setVisibility(View.GONE);
        }
    }

    private void setSingleNavBtn(Button btn, int res) {
        if (res > 0) {
            btn.setBackgroundResource(res);
        } else {
            btn.setVisibility(View.GONE);
        }
    }

    private void setSingleNavBtn(Button btn, int res, String value) {
        if (res > 0) {
            btn.setBackgroundResource(res);
        } else if (!TextUtils.isEmpty(value)) {
            btn.setText(value);
        } else {
            btn.setVisibility(View.GONE);
        }
    }
}
